package pl.iachoo.ctl.tabory.dao.hibernate.services;

import java.util.List;

import org.apache.log4j.Logger;

import pl.iachoo.ctl.tabory.dao.hibernate.interfaces.IUserService;
import pl.iachoo.ctl.tabory.dao.hibernate.vo.UserVO;

public class UserServiceImpl extends BaseDaoService<UserVO> implements
		IUserService {

	private final static Logger log = Logger.getLogger(UserServiceImpl.class);

	private static UserServiceImpl instance;

	private UserServiceImpl() {

	}

	public static UserServiceImpl getInstance() {
		if (instance == null) {
			log.info("Creating new instance of service.");
			instance = new UserServiceImpl();
		}
		log.info("Retriving service: UserServiceImpl.");
		return instance;
	}

	@Override
	public UserVO getUser(int id) throws Exception {
		log.debug("Getting User for ID: " + id);
		return load(UserVO.class, new Integer(id));
	}

	@Override
	public UserVO getUser(String userLogon) throws Exception {
		log.debug("Getting User for userLogon: " + userLogon);
		UserVO criterion = new UserVO();
		criterion.setUserLogon(userLogon);
		List<UserVO> result = loadList(UserVO.class, criterion);
		if (result == null || (result != null && result.isEmpty())) {
			return null;
		} else if (result.size() > 1) {
			throw new RuntimeException("To many user for given logon");
		}
		return result.get(0);
	}

}
